let API_URL = 'http://localhost:8080'

window.getUser = async function () {
    try {
        document.getElementById("getResult").innerHTML = "加载中..."
        let response = await fetch(API_URL + '/users/1')
        let user = await response.json()
        document.getElementById('getResult').innerHTML = `用户:${user.name}<br>邮箱:${user.email}<br>电话:${user.phone}`
        console.log(user)

    } catch (error) {
        document.getElementById("getResult").innerHTML = "获取失败..."
        console.log("获取用户失败:", error)
    }
}

window.postMessage = async function () {
    try {
        let title = document.getElementById("title").value || '默认标题'
        let content = document.getElementById("content").value || "默认内容"
        document.getElementById("postResult").innerHTML = "提交中..."

        let response = await fetch(API_URL + '/posts', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                title: title,
                body: content,
                userId: 1
            })
        })

        let result = await response.json()
        document.getElementById('postResult').innerHTML = `创建成功! ID: ${result.id}<br>标题: ${result.title}`
        console.log(result)

    } catch (error) {
        document.getElementById("postResult").innerHTML = "提交失败。。。"
        console.log("提交失败:", error)
    }
}